﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class LoaiMonAnDAO
    {
        public LoaiMonAnDTO LayThongTinLoaiMonAn(string MaLMA)
        {
            LoaiMonAnDTO mal = new LoaiMonAnDTO();
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();

            SqlCommand cmd = new SqlCommand("proc_LayLoaiMonAnTheoMa", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter sqlPar;
            sqlPar = new SqlParameter("@malma", SqlDbType.VarChar, 6);
            sqlPar.Direction = ParameterDirection.Input;
            sqlPar.Value = MaLMA;
            cmd.Parameters.Add(sqlPar);
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            mal.MaLoaiMA = reader.GetString(0);
            mal.TenLoaiMA = reader.GetString(1);
            mal.MaHop = reader.GetString(2);
            mal.DonGia = reader.GetDouble(3);
            conn.Close();
            return mal;
        }

        public List<LoaiMonAnDTO> DanhSachLoaiMonAn()
        {
            List<LoaiMonAnDTO> ds = new List<LoaiMonAnDTO>();
            SqlConnection conn = new SqlConnection();
            conn = DataProvider.ConnectDB();

            SqlCommand cmd = new SqlCommand("proc_DanhSachLoaiMonAn", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                LoaiMonAnDTO mal = new LoaiMonAnDTO();
                mal.MaLoaiMA = reader.GetString(0);
                mal.TenLoaiMA = reader.GetString(1);
                mal.MaHop = reader.GetString(2);
                mal.DonGia = reader.GetDouble(3);
                ds.Add(mal);

            }
            conn.Close();
            return ds;
        }
    }
}
